Mapped Type for array and tuple
このプルリクに習って、このタイトルでページを作った
In TypeScript 3.1, mapped object types over tuples and arrays now produce new tuples/arrays, rather than creating a new type where members like push(), pop(), and length are converted. For example:
code:ts
このように、タプルや配列をkeyofすると、配列のpushとかpopとかの関数がkeyとして出現してしまっていたが、そこはよしなに見てindexの数字のみをkeyに取るようにした、というもの
Array Likeな型のMapped Typeは、その配列の要素に対してマッピングが行われる
ただし、これが動かない場合がある
code:ts
interface Bar
{
a: string;
b: number;
}
これは動かん
The issue here is that we only map to tuple and array types when we instantiate a generic homomorphic mapped type for a tuple or array.
どうやら、keyofがジェネリックなときだけ動いてて、この状況では現状動かないっぽい?
genericな同型のmapped typeをインスタンス化したときだけ
どういうこと?
code:ts
interface Box<V = unknown> {
value: V
}
function box<V>(value: V) {
return { value }
}
type Unbox<B extends Box> = B'value' type UnboxAll<T extends Box[]> = {
}
declare function unboxArray<T extends Box[]>(boxesArray: T): UnboxAll<T>
declare function unboxTuple<T extends Box[]>(...boxesTuple: T): UnboxAll<T>
Extract< T[i], t[number]>しないと、こうなる
https://gyazo.com/f6ae552e4e0109bfed606553d4db52a4